package com.controller;

import com.dao.UserDAO;
import com.dao.impl.UserDAOImpl;
import com.vo.User;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/tihai")
public class MainController {
    private final UserDAO userDAO;

    // 使用构造器注入
    public MainController(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    @GetMapping
    public String gotoMain() {
        return "tihai/main";
    }

    @GetMapping("/game")
    public String gotoGame(){
        return "tihai/game";
    }

    @GetMapping("/edit")
    public String editPage() {
        return "tihai/edit"; // 返回编辑页面
    }

    @PostMapping("/edit")
    public String edit(@RequestParam String account,
                       @RequestParam String userPwd,
                       @RequestParam String userName,
                       @RequestParam String sex,
                       @RequestParam String introduction,
                       HttpSession session,
                       Model model) {
        User updataUser = userDAO.findStudentByAccount(account);
        updataUser.setUserPwd(userPwd);
        updataUser.setUserName(userName);
        updataUser.setSex(sex);
        updataUser.setIntroduction(introduction);
        userDAO.update(updataUser);
        session.setAttribute("currentUser", updataUser);
        model.addAttribute("success", "修改成功");
        return "redirect:/tihai";
    }
}
